package com.jiutong.meipenggang.ui.activitys;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.ui.fragments.NoUseHongBaoFragment;
import com.jiutong.meipenggang.ui.fragments.OverHongBaoFragment;
import com.jiutong.meipenggang.ui.fragments.UsedHongBaoFragment;
import com.jiutong.meipenggang.utils.DensityUtil;
import com.jiutong.meipenggang.views.TextChangeColorView;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author Veken
 *         作用：红包页面 采用字体变色的LinerLayout+ViewPager实现页面的切换
 */
public class HongbaoActivity extends BaseActivity {

    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.line1)
    View line1;
    @Bind(R.id.fl_hongbao_list)
    FrameLayout flHongbaoList;
    @Bind(R.id.vPager_hongbao)
    ViewPager vPagerHongbao;
    @Bind(R.id.tv_common_title_options)
    TextView tvCommonTitleOptions;
    @Bind(R.id.ll_hongbao)
    LinearLayout llHongBao;

    private FragmentStatePagerAdapter pagerAdapter;
    private int width1_3;
    private int width;
    private int mCurrentIndex;
    private Display display1;
    private DisplayMetrics outMetrics;
    private List<TextChangeColorView> mList = new ArrayList<>();
    private String[] str = new String[]{"未使用", "已使用", "已过期"};
    private TextChangeColorView tx;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_hongbao;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("我的红包");
        tvCommonTitleOptions.setText("说明");
        tvCommonTitleOptions.setVisibility(View.VISIBLE);
        tvCommonTitleOptions.setTextColor(getResources().getColor(R.color.default_gray));

        //获得屏幕宽度
        display1 = getWindow().getWindowManager().getDefaultDisplay();
        outMetrics = new DisplayMetrics();
        display1.getMetrics(outMetrics);
        //定义下划线宽度为屏幕的三分之一
        width1_3 = outMetrics.widthPixels / 3;
        width = DensityUtil.dip2px(this, 70);
        initIndicator();
        NoUseHongBaoFragment noUseHongBaoFragment = new NoUseHongBaoFragment();
        UsedHongBaoFragment usedHongBaoFragment = new UsedHongBaoFragment();
        OverHongBaoFragment overHongBaoFragment = new OverHongBaoFragment();
        fragmentList.add(noUseHongBaoFragment);
        fragmentList.add(usedHongBaoFragment);
        fragmentList.add(overHongBaoFragment);
        initViewPager();

    }

    @Override
    protected void onResume() {
        super.onResume();
        vPagerHongbao.setCurrentItem(mCurrentIndex);
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        //初始下划线marginleft
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) line1.getLayoutParams();
        lp.width = width;
        lp.leftMargin = (width1_3 - width) / 2;
        line1.setLayoutParams(lp);
        pagerAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        vPagerHongbao.setOffscreenPageLimit(3);
        vPagerHongbao.setAdapter(pagerAdapter);
        vPagerHongbao.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //根据滑动的positionOffset设置leftMargin
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) line1.getLayoutParams();
                //0->1
                if (mCurrentIndex == 0 && position == 0) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + width1_3 * positionOffset) + (width1_3 - width) / 2;
                } //1->0
                else if (mCurrentIndex == 1 && position == 0) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + (positionOffset - 1) * width1_3) + (width1_3 - width) / 2;
                } //1->2
                else if (mCurrentIndex == 1 && position == 1) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + positionOffset * width1_3) + (width1_3 - width) / 2;
                } //2->1
                else if (mCurrentIndex == 2 && position == 1) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + (positionOffset - 1) * width1_3) + (width1_3 - width) / 2;
                }
                lp.width = width;
                line1.setLayoutParams(lp);

                TextChangeColorView left = mList.get(position);
                left.setmDirection(TextChangeColorView.Direction.RIGHT_TO_LEFT);
                left.setmCurrentProgress(1 - positionOffset);
                if (position == 2) {
                    return;
                }
                TextChangeColorView right = mList.get(position + 1);
                right.setmCurrentProgress(positionOffset);
                right.setmDirection(TextChangeColorView.Direction.LEFT_TO_RIGHT);

            }

            @Override
            public void onPageSelected(int position) {
                if(tx.isClickable()){
                    TextChangeColorView tccvLast = mList.get(mCurrentIndex);
                    TextChangeColorView tccvCurrent = mList.get(position);

                    tccvLast.setChangeColor(getResources().getColor(R.color.hongbao_table_gray));
                    tccvLast.invalidate();

                    tccvCurrent.setChangeColor(getResources().getColor(R.color.gesture_color));
                    tccvCurrent.invalidate();
                }

                mCurrentIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    /**
     * 添加字体变色的样式
     */
    private void initIndicator() {
        for (int i = 0; i < str.length; i++) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.weight = 1;
            tx = new TextChangeColorView(this);
            tx.setText(str[i]);
            tx.setTextSize(20);
            tx.setLayoutParams(lp);
            llHongBao.addView(tx);
            mList.add(tx);
            //字体的点击监听
            final int index = i;
            tx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vPagerHongbao.setCurrentItem(index, true);
                }
            });
        }
    }

    @OnClick({R.id.rl_common_title_back, R.id.tv_common_title_options})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.tv_common_title_options:
                openActivity(ExplainActivity.class);
                break;
            default:
                break;
        }
    }

}
